<template>
<div>
    <!-- 登录头部 -->
    <van-nav-bar title="黑马头条-登录"/>
    <!--登录表单  -->
    <van-form @submit="onSubmit">
  <van-field
    v-model="formLogin.mobile"
    name="用户"
    label="手机号码"
    required
    placeholder="请输入手机号码"
    :rules="[{ required: true, message: '请填写用户名',trigger:'onChange',
    pattern:/^1[3-9]\d{0,9}$/}]"
  />
  <van-field
    v-model="formLogin.code"
    type="password"
    name="密码"
    required
    label="登录密码"
    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写密码',trigger:'onChange',pattern:/^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" loading-text="加载中..."  :loading="isloading" :disabled="isloading">登录</van-button>
  </div>
</van-form>
</div>
</template>

<script>
import { loginAPI } from '@/api'
import { Mytitle } from '@/utils/Notify.js'
import { mapMutations } from 'vuex'

export default {
  name: 'Login',
  data () {
    return {
      formLogin: {
        mobile: '19900000000',
        code: '246810'
      },
      isloading: false
    }
  },
  methods: {
    ...mapMutations(['setToken', 'setRefreshToken']),
    // 点击登录
    async onSubmit () {
      this.isloading = true
      try {
        const res = await loginAPI(this.formLogin)
        this.setToken(res.data.data.token)
        this.setRefreshToken(res.data.data.refresh_token)
        Mytitle({ type: 'success', message: '登录成功' })
        this.isloading = false
        if (this.$route.query.code === '401') {
          this.$router.back()
        } else {
          this.$router.push({
            path: '/layout'
          })
        }
      } catch (err) {
        console.log(err)
        Mytitle({ type: 'warning', message: '登录失败' })
        this.isloading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 第一种添加样式方法
// .van-nav-bar{
//     background-color:#007bff;
// }
// /deep/ .van-nav-bar__title{
//    color:white;
// }
</style>
